using Decal.Adapter;
using Decal.Adapter.Wrappers;
using System;
using System.IO;

namespace SixthSense
{
    public partial class PluginCore : PluginBase
    {
        private ViewWrapper _MainView;
        private ViewWrapper _EditView;
        private Config _Config = new Config();
        private string _ConfigFile;
        private string _Path;

        public string errorLogFile { get { return Path + "errorLog.txt"; } }

        protected override void Startup()
        {
            //_MainView = Host.LoadViewResource("SixthSense.ViewXML.Schema.xml");

            if (!pluginPersonalFolder.Exists)
                pluginPersonalFolder.Create();

            _Path = pluginPersonalFolder.ToString();
            _ConfigFile = _Path + "\\config.xml";

            FileInfo file = new FileInfo(_ConfigFile);

            if (!file.Exists)
            {
                Stream s = this.GetType().Assembly.GetManifestResourceStream("SixthSense.Config.xml");

                using (FileStream fs = File.Create(_ConfigFile))
                {
                    Byte[] buffer = new byte[s.Length];
                    s.Read(buffer, 0, (int)s.Length);
                    fs.Write(buffer, 0, buffer.Length);
                }
            }

            try
            {
                _Config.Load(_ConfigFile);

                initCharStats();
                initEchoFilter();
                initWorldFilter();
                initChatEvents();
            }
            catch (Exception ex)
            {
                errorLogging.LogError(errorLogFile, ex);
            }
        }

        protected override void Shutdown()
        {
            try
            {
                destroyChatEvents();
                destroyCharStats();
                destroyWorldFilter();
                destroyEchoFilter();
            }
            catch (Exception ex)
            {
                errorLogging.LogError(errorLogFile, ex);
            }
        }

        private static DirectoryInfo pluginPersonalFolder = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\Decal Plugins\Sixth Sense\");
    }
}